import java.util.ArrayList;

public class Typing extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;


	public static void main(String[] args) {
		
		ArrayList<Integer> jsReading = new ArrayList<Integer>();
		String inputMsg;
		
		SocWear socWear = new SocWear(new GUI(), "D:/msg.txt");
		socWear.connectToiCATch("COM5");
		Joystick joystick = (Joystick)socWear.dm.getDeviceByType(Device.JOYSTICK);
		ResistanceSensor rs = (ResistanceSensor)socWear.dm.getDeviceByType(Device.RESISTANCE_SENSOR);
		LED led = (LED)socWear.dm.getDeviceByType(Device.LED);
		inputMsg = "";
		
		led.on(LED.RED);
		
		while (true) {
			jsReading.clear();
			int rsRe = rs.getReading();
		
			System.out.println("rsRe is "+rsRe);
			
			if (rsRe > 150) {
				SocWear.sleep(200);
				led.on(LED.RED);
			}
			
				while (rsRe > 100){
					SocWear.sleep(200);
					int jsRe = joystick.getDirection();
					System.out.println("joystick : " + jsRe);
					
					if (jsRe > 1 && jsRe <= 5) {
						jsReading.add(jsRe);
					}

					if (jsRe == 1){
						System.out.println("joystick is pushed down!");
						SocWear.sleep(200);
						led.off(LED.RED);
						
						UserInput(jsReading, inputMsg);
						
						break;
					}
				}
			SocWear.sleep(200);
		}
	}
	

	private static void UserInput(ArrayList<Integer> jsReading, String inputMsg){
		char[] char1 = {'a','b','c','d','e','f','g'};
		char[] char2 = {'i','j','k','l','m','n','o'};
		char[] char3 = {'p','q','r','s','t','u','v'};
		char[] char4 = {'w','x','y','z',' '};
		System.out.println("arrayList is" + jsReading);
		int[] count = {0,0,0,0};
		int i = 1;
		int previous = 0;
		
		for (int j = 0; j < jsReading.size(); j++) {
			
			int value = jsReading.get(j);
			
			if (value != previous && value!=5) {
				count[jsReading.get(j)-1]++;
			}
			
			previous = jsReading.get(j);
		}

		for (int j : count) {
			System.out.println(i + ":" + j);
			if (j > 0){
				switch (i){
					case 1:
						inputMsg += char1[j-1];
						break;
					case 2:
						inputMsg += char2[j-1];
						break;
					case 3:
						inputMsg += char3[j-1];
						break;
					case 4:
						inputMsg += char4[j-1];
						break;
				} 
			}
			i++;
		}
		
		
		System.out.println("Input Message : "+inputMsg);
		TwitterManager tm = new TwitterManager();
		tm.connectToTwitter();
		tm.tweet(inputMsg);
		
	}
}